﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace IReklamo
{
    /// <summary>
    /// Summary description for UploadHandler
    /// </summary>
    public class ComplaintUploadHandler : UploadHandler
    {
        public override void ProcessData(HttpContext context, ProcessData data)
        {
            var repository = new ComplaintRepository();
            var dynamicData = data.FormData as System.Collections.Specialized.NameValueCollection;

            data.Files.ForEach(file =>
            {
                var thumbsFolderName = "thumbs";
                var sourceFolder = "/images/complaints/";
                var sourceAbsoluteFolder = HttpContext.Current.Server.MapPath(sourceFolder);
                var actualFile = string.Concat(sourceAbsoluteFolder, file.name);
                var thumbFileName = ImageHelper.GenerateThumbNail(actualFile, string.Concat(sourceAbsoluteFolder, thumbsFolderName, @"\"), 125, 125);
                repository.AddVisualToComplaint(
                    new Visual
                    {
                        ComplaintId = Convert.ToInt32(dynamicData["id"]),
                        ThumbFile =  string.Concat(sourceFolder,  thumbsFolderName, "/", thumbFileName),
                        ActualFile = string.Concat(sourceFolder, Path.GetFileName(file.name))
                    }
                );
            });
        }
    }
}